using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0680. 验证回文字符串 Ⅱ")]
    public class No0680_ValidPalindrome : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(ValidPalindrome, "aba", true);
            Assert.TestExe(ValidPalindrome, "abca", true);
            Assert.TestExe(ValidPalindrome, "abcda", false);
            Assert.TestExe(ValidPalindrome, "abccba", true);
            Assert.TestExe(ValidPalindrome, "abccbda", true);
            Assert.TestExe(ValidPalindrome, "abcdcba", true);
            Assert.TestExe(ValidPalindrome, "fbcdcba", false);
            Assert.TestExe(ValidPalindrome, "fabcdcba", true);
            Assert.TestExe(ValidPalindrome, "eeccccbebaeeabebccceea", false);
            Assert.TestExe(ValidPalindrome, "deeee", true);
        }

        public bool ValidPalindrome(string s)
        {
            int min = 0;
            int max = s.Length - 1;
            while (min < max)
            {
                if (s[min] == s[max])
                {
                    min++;
                    max--;
                }
                else
                {
                    bool flag1 = true;
                    for (int i = min, j = max - 1; i < j; i++, j--)
                    {
                        if (s[i] != s[j])
                        {
                            flag1 = false;
                            break;
                        }
                    }
                    bool flag2 = true;
                    for (int i = min + 1, j = max; i < j; i++, j--)
                    {
                        if (s[i] != s[j])
                        {
                            flag2 = false;
                            break;
                        }
                    }
                    return flag1 || flag2;
                }
            }
            return true;
        }
    }
}
